import { hsl } from "d3";
export default {
  fetchEntityTypes() {
    // const entityTypes = entityTypesKey.map((item, index) => ({
    //     name: graphInfo.entity_types[item].name,
    //     code: graphInfo.entity_types[item].entity_type,
    //     color: graphInfo.entity_types[item].style?.color ? graphInfo.entity_types[item].style.color.toString() : hsl(Math.floor((index / entityTypesKey.length) * 360), 0.8, 0.7).toString(),
    //     colorLight: graphInfo.entity_types[item].style?.colorLight ? graphInfo.entity_types[item].style.colorLight.toString() : hsl(
    //         Math.floor((index / entityTypesKey.length) * 360),
    //         0.8,
    //         0.9,
    //     ).toString(),
    // }));
    const entityTypes = [
      {
        name: "技术",
        // color: hsl(Math.floor((0 / 1) * 360), 0.8, 0.7).toString(),
        // color: 'rgba(117,189,240,1)',
        color: "#7a74d3",
        // colorLight: hsl(
        //     Math.floor((0 / 1) * 360),
        //     0.8,
        //     0.9,
        colorLight: "#524cab",
        activeColor: "yellow"
      }
    ];
    return entityTypes;
  },
  processData(data, rootEntity) {
    if (!data) {
      return;
    }
    const entityTypes = this.fetchEntityTypes();
    //设置中心节点大小
    if (rootEntity) {
      const rootEntityOrder = data.node.findIndex(
        item => item.name === rootEntity
      );
      if (rootEntityOrder > -1) {
        data.node[rootEntityOrder].size = 35;
      }
    }
    //设置一级节点大小
    if (data.node && data.link) {
      data.link.forEach(link => {
        if (link === null) return true;
        if (link.s_name === rootEntity) {
          const targetNodeOrder = data.node.findIndex(
            item => item.eid === link.o_eid
          );
          if (targetNodeOrder > -1) {
            data.node[targetNodeOrder].size = 20;
          }
        } else if (link.o_name === rootEntity) {
          const targetNodeOrder = data.node.findIndex(
            item => item.eid === link.s_eid
          );
          if (targetNodeOrder > -1) {
            data.node[targetNodeOrder].size = 20;
          }
        }
      });
      data.node.forEach(node => {
        node.size = node.size ? node.size : 12;
      });
      return {
        nodes: data.node.map(item => ({
          id: item.eid,
          etype: item.etype,
          label: item.name,
          size: item.size,
          style: {
            fill: entityTypes.find(type => type.name === item.etype)?.color,
            lineWidth: 0
          },
          stateStyles: {
            hover: {
              fill: entityTypes.find(type => type.name === item.etype)
                ?.colorLight
            },
            active: {
              fill: "yellow"
            },
            putAsh: {
              opacity: 0.1
            }
          }
        })),
        edges: data.link.map(item => ({
          label: item.rel_type + "(" + item.count + ")",
          source: item.s_eid,
          target: item.o_eid,
          targetName: item.o_name,
          sourceName: item.s_name,
          style: {
            stroke: "#d2d2d2",
            lineWidth: 1,
            lineAppendWidth: 5
          },
          stateStyles: {
            hover: {
              stroke: "#1890ff"
            },
            putAsh: {
              opacity: 0.1
            }
          }
        }))
      };
    }
    return data;
  }
};
